$(function(){
	// 加载表格
	contractMngTable = $("#contractMngTable").DataTable(dataTableConfig);
});
// 后面构建btn 代码
var btnModel = '    \
	{{#each func}}\
    <button type="button" class="{{this.type}} btn-sm" onclick="{{this.fn}}">{{this.name}}</button>\
    {{/each}}';
var template = Handlebars.compile(btnModel);
var contractMngTable;
// 表格配置信息
var dataTableConfig = {
	"ordering": false,// 排序
	"serverSide": true,// 开启服务器模式
	"scrollX": true,// 横向滚动
	ajax: {
        "type": "POST",
        "url":'qryTrlContMdlList',//请求路径
        "contentType": 'application/x-www-form-urlencoded; charset=UTF-8',
        "dataType":'json',
        "data":function(d){// 查询参数
        	d.contractName = $('#qryContractName').val();
        	return d;
        }
	},            
	columns: [// 对应列
		{"data": "contractName","title":"合同模板名称",className: "text-center",
			"render": function ( data, type, full, meta ) 
				{return type === 'display' && data.length > 35 ?
					        '<span title="'+data+'">'+data.substr( 0, 33 )+'...</span>' :
					        data;
					    }},
        {"data": "uploadStaffId","title":"上传人",className: "text-center"},
        {"data": "uploadDate","title":"上传时间",className: "text-center",render: function (a, b, c) {
        	return a!=null?new Date(a.time).Format("yyyy-MM-dd hh:mm:ss"):"";
        }},
        {"data": "status","title":"状态",className: "text-center",render: function (a, b, c, d){
			if('1' == c.status)return '有效';else return '无效';
		}},
		{"data": "remark","title":"备注",className: "text-center"},
        {
			"data" : null,
			"title" : "操作",
			className : "text-center",
			render : function(a, b, c, d) {
				var btnArray = new Array();
				if(menuType==0){
    				btnArray.push({"name": "删除", "fn": "delContractTmp(\'"+c.contractId+"\')", "type": "user-button user-btn-n"});
				}
    			btnArray.push({"name": "下载", "fn": "downloadContractTmp(\'"+c.contractId+"\')", "type": "user-button user-btn-n"});
				context = {
					func : btnArray
				}
				var html = template(context);
				return html;
			}
		}],
		"columnDefs" : [ {// 所有列默认值
			"targets" : "_all",
			"defaultContent" : ''
		// render: $.fn.dataTable.render.ellipsis(22, true)
		} ],
		"dom" : 'rt<"pull-left mt5"l><"pull-left mt4"i><"pull-right mt5"p><"clear">' // 生成样式
		};


Date.prototype.Format = function (fmt) { // author: meizz
    var o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


function downloadContractTmp(contractId){
	var url = "downloadContractTmp?contractId="+contractId;
	var iframe = $("#_url").attr("src", url);
}

function delContractTmp(contractId){
	if (confirm("确认要删除该附件吗?")) {
		$.ajax({
	        url: "delContractTmp",
	        data: "contractId=" + contractId,
	        dataType: "json",
	        type: "post",
	        success: function(data) {
	        	if (data) {
	        		$("#contractMngTable").DataTable().ajax.reload();
	        		layer.alert("删除成功");
	        	} else {
	        		layer.alert("删除失败");
	        	}
	        }
		});
    }
}

$(function(){
	contractMngTable = $("#contractMngTable").DataTable(dataTableConfig);//页面加载时渲染表格
})

function serarchContract(){
	$("#contractMngTable").DataTable().ajax.reload();
}

//重置所用条件查询输入框
function reset(){
	$('#contractMngForm input').val('');	
}

function uploadContractTmp(){
	var myfile = $("#myfile").val();
	var contractName = $("#contractName").val();
	if(""==myfile || ""==contractName){
		layer.alert("信息填写不完整！");
		return;
	}
	var fileArr = myfile.split(".");
	var fileSuffix = fileArr[fileArr.length-1];
	if(fileSuffix=="txt" || fileSuffix=="xls" || fileSuffix=="xlsx" || fileSuffix=="doc" || fileSuffix=="docx"){
		var dom = document.getElementById("myfile");  
		var fSize;
		if(dom.files){
			fSize =  dom.files[0].size;
		}
		else{
			fSize = 10;
		}
		var MB = (100*1024*1024).toString();
		if(fSize>MB){
			layer.alert("上传文件大小超出限制,请上传小于100M的文件");
		}else{
			
			$.ajaxFileUpload({
				url: "uploadContractTmp",
				type: "post",
				data: { contractName: $("#contractName").val(), remark: $("#remark").val()},
				fileElementId: "myfile",
				dataType: "text/html",
				success: function(data) {
		  
					var obj = JSON.parse(data);
					if(obj.res=="success"){
						layer.alert(obj.msg);
						$("#uploadContModal").modal('hide');
						$("#contractMngTable").DataTable().ajax.reload();
					}else if(obj.res=="error"){
						layer.alert(obj.msg);
					}
				}
			});
		}
	}else{
		layer.alert("只能上传txt、xls、xlsx、doc、docx文件！");
	}
}